import Vue from 'vue';
import VueRouter from 'vue-router';
// import Index from '../views/Index.vue';

import { Toast } from 'vant';
Vue.use(Toast);

Vue.use(VueRouter);

const routes = [
  {
    path: '/deilydetial',
    name: 'deilydetial',
    props: true,
    component: () => import('../views/DailyDetial.vue'),
  },
  {
    path: '/contactedit',
    name: 'contactedit',
    props: true,
    component: () => import('../views/ContactEdit.vue'),
  },
  {
    path: '/contactadd',
    name: 'contactadd',
    props: true,
    component: () => import('../views/ContactAdd.vue'),
  },
  {
    path: '/contactlist',
    // 开启路由接参
    props: true,
    name: 'contactlist',
    component: () => import('../views/ContactList.vue'),
  },
  {
    path: '/petreservation',
    // 开启路由接参
    props: true,
    name: 'petreservation',
    component: () => import('../views/PetReservation.vue'),
  },
  {
    path: '/servicedetails',
    //订单详情页
    path: '/servicedetails/',
    // 开启路由接参
    props: true,
    name: 'servicedetails',
    component: () => import('../views/ServiceDetails.vue'),
  },

  {
    path: '/business/:b_id',
    // 开启路由接参
    props: true,
    name: 'business',
    component: () => import('../views/Business.vue'),
  },
  {
    path: '/petfiles',
    // 开启路由接参
    props: true,
    name: 'petfiles',
    component: () => import('../views/PetFiles.vue'),
  },
  {
    path: '/petfileschange',
    // 开启路由接参
    props: true,
    name: 'petfileschange',
    component: () => import('../views/PetFilesChange.vue'),
  },
  {
    path: '/',
    name: '',
    redirect: '/login',
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/index',
    name: 'index',
    meta: { showFooter: true },
    component: () => import('../views/Index.vue'),
  },
  {
    path: '/register',
    name: 'register',
    meta: { showFooter: false },
    component: () => import('../views/Register.vue'),
  },
  {
    path: '/login',
    name: 'login',
    meta: { showFooter: false },
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/me',
    name: 'me',
    meta: { showFooter: true },
    component: () => import('../views/Me.vue'),
  },
  ,
  {
    path: '/order',
    name: 'order',
    meta: { showFooter: true },
    component: () => import('../views/Order.vue'),
  },
  {
    path: '/board',
    name: 'board',
    meta: { showFooter: true },
    component: () => import('../views/Board.vue'),
  },
  {
    path: '/dailylist',
    name: 'dailylist',
    component: () => import('../views/DailyList.vue'),
  },
];

const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: routes,
  scrollBehavior(to, from, savedPosition) {
    // console.log(savedPosition)
    if (savedPosition) {
      return savedPosition;
    } else {
      return {
        x: 0,
        y: 0,
      };
    }
  },
});
// 前置路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register') {
    next();
  } else {
    let token = sessionStorage.getItem('token');

    if (token === null || token === '') {
      Toast({
        message: '请您先登录',
        icon: require('../assets/img/logo1.png'),
      });

      next('/login');
    } else {
      next();
    }
  }
});

export default router;
